//
//  LBBilateralFilter.mm
//  OpenGLGame
//
//  Created by Nguyen Cong Huy on 2/27/13.
//  Copyright (c) 2013 UIT. All rights reserved.
//

#ifndef OpenGLGame_LBBilateralFilter_mm
#define OpenGLGame_LBBilateralFilter_mm

#include "LBBilateralFilter.h"
#include "LBGaussianBlurFilter.h"

char bilateralFragShaderStr[] = ""
"precision mediump float;"

"uniform sampler2D texture;"

"const lowp int GAUSSIAN_SAMPLES = 9;"

"varying vec2 vtexCoor;"
"varying vec2 blurCoordinates[GAUSSIAN_SAMPLES];"

"const mediump float distanceNormalizationFactor = 1.5;"

"void main()"
"{"
"    lowp vec4 centralColor = texture2D(texture, blurCoordinates[4]);"
"    lowp float gaussianWeightTotal = 0.18;"
"    lowp vec4 sum = centralColor * 0.18;"

"    lowp vec4 sampleColor = texture2D(texture, blurCoordinates[0]);"
"    lowp float distanceFromCentralColor;"
"    distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);"

"    lowp float gaussianWeight = 0.05 * (1.0 - distanceFromCentralColor);"
"    gaussianWeightTotal += gaussianWeight;"
"    sum += sampleColor * gaussianWeight;"

"    sampleColor = texture2D(texture, blurCoordinates[1]);"
"    distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);"
"    gaussianWeight = 0.09 * (1.0 - distanceFromCentralColor);"
"    gaussianWeightTotal += gaussianWeight;"
"    sum += sampleColor * gaussianWeight;" 

"    sampleColor = texture2D(texture, blurCoordinates[2]);"
"    distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);"
"    gaussianWeight = 0.12 * (1.0 - distanceFromCentralColor);"
"    gaussianWeightTotal += gaussianWeight;"
"    sum += sampleColor * gaussianWeight;"

"    sampleColor = texture2D(texture, blurCoordinates[3]);"
"    distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);"
"    gaussianWeight = 0.15 * (1.0 - distanceFromCentralColor);"
"    gaussianWeightTotal += gaussianWeight;"
"    sum += sampleColor * gaussianWeight;"

"    sampleColor = texture2D(texture, blurCoordinates[5]);"
"    distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);"
"    gaussianWeight = 0.15 * (1.0 - distanceFromCentralColor);"
"    gaussianWeightTotal += gaussianWeight;"
"    sum += sampleColor * gaussianWeight;"

"    sampleColor = texture2D(texture, blurCoordinates[6]);"
"    distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);"
"    gaussianWeight = 0.12 * (1.0 - distanceFromCentralColor);"
"    gaussianWeightTotal += gaussianWeight;"
"    sum += sampleColor * gaussianWeight;"

"    sampleColor = texture2D(texture, blurCoordinates[7]);"
"    distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);"
"    gaussianWeight = 0.09 * (1.0 - distanceFromCentralColor);"
"    gaussianWeightTotal += gaussianWeight;"
"    sum += sampleColor * gaussianWeight;"

"    sampleColor = texture2D(texture, blurCoordinates[8]);"
"    distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);"
"    gaussianWeight = 0.05 * (1.0 - distanceFromCentralColor);"
"    gaussianWeightTotal += gaussianWeight;"
"    sum += sampleColor * gaussianWeight;"

"    gl_FragColor = sum / gaussianWeightTotal;"

"}";

LBBilateralFilter::LBBilateralFilter():LBGaussianBlurFilter(nil, bilateralFragShaderStr, nil, bilateralFragShaderStr){
}

#endif
